﻿//
// Copyright (C) Ashish Kaila 2011
//

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Flipbook.Commands;
using Flipbook.Controls;
using Flipbook.ViewModels;
using MixModes.Synergy.VisualFramework.Commands;
using System;

namespace Flipbook
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            ZoomOutCommand = new CommandBase(arg => ZoomOut(arg as ManipulationDeltaEventArgs), arg => CanZoomOut(arg as ManipulationDeltaEventArgs));
            ResetRotationCommand = new CommandBase(arg => ResetRotation());
            DisplayCanvasSubsetCommand = new CommandBase(arg => DisplayCanvasSubset(arg as GestureCommandParameter));
            _applicationViewModel = new ApplicationViewModel();
            _radialViewControl = new RadialViewControl();
            _radialViewControl.Close += (a, b) => { MainGrid.Children.Remove(_radialViewControl); };
            InitializeComponent();
            DataContext = _applicationViewModel;
        }

        /// <summary>
        /// Gets or sets the reset rotation command.
        /// </summary>
        /// <value>
        /// The reset rotation command.
        /// </value>
        public ICommand ResetRotationCommand { get; set; }

        /// <summary>
        /// Gets or sets the display canvas subset command.
        /// </summary>
        /// <value>
        /// The display canvas subset command.
        /// </value>
        public ICommand DisplayCanvasSubsetCommand { get; set; }

        /// <summary>
        /// Gets or sets the zoom out command.
        /// </summary>
        /// <value>
        /// The zoom out command.
        /// </value>
        public ICommand ZoomOutCommand { get; set; }

        /// <summary>
        /// Resets the rotation.
        /// </summary>
        private void ResetRotation()
        {
            CanvasGrid.RenderTransform = null;
        }

        /// <summary>
        /// Displays the canvas subset.
        /// </summary>
        /// <param name="gestureCommandParameter">The gesture command parameter.</param>
        private void DisplayCanvasSubset(GestureCommandParameter gestureCommandParameter)
        {
            CanvasViewModel canvasViewModel;

            if ((gestureCommandParameter == null) || 
                ((canvasViewModel = gestureCommandParameter.Parameter as CanvasViewModel) == null))
            {
                return;
            }

            int index = _applicationViewModel.CanvasViewModels.IndexOf(canvasViewModel);

            if (index < 0)
            {
                return;
            }

            if (_canvasSubsetControl == null)
            {
                _canvasSubsetControl = new CanvasSubsetControl();
                _canvasSubsetControl.DataContext = _applicationViewModel;
                Canvas.Children.Add(_canvasSubsetControl);
            }            
        }

        /// <summary>
        /// Zooms out the view
        /// </summary>
        /// <param name="manipulationDeltaEventArgs">The <see cref="System.Windows.Input.ManipulationDeltaEventArgs"/> instance containing the event data.</param>
        private void ZoomOut(ManipulationDeltaEventArgs manipulationDeltaEventArgs)
        {
            FrameworkElement element = manipulationDeltaEventArgs.Source as FrameworkElement;

            if ((element != null) && (element.RenderTransform != null))
            {
                element.RenderTransform = null;
            }

            MainGrid.Children.Remove(_radialViewControl);
            _radialViewControl.DataContext = _applicationViewModel;
            _radialViewControl.Height = MainGrid.ActualHeight;
            _radialViewControl.Width = MainGrid.ActualWidth;
            Grid.SetRow(_radialViewControl, 1);
            MainGrid.Children.Add(_radialViewControl);
        }

        /// <summary>
        /// Determines whether view can zoom out based on the specified manipulation delta event args.
        /// </summary>
        /// <param name="manipulationDeltaEventArgs">The <see cref="System.Windows.Input.ManipulationDeltaEventArgs"/> instance containing the event data.</param>
        /// <returns></returns>
        private bool CanZoomOut(ManipulationDeltaEventArgs manipulationDeltaEventArgs)
        {            
            ManipulationDelta deltaManipulation = manipulationDeltaEventArgs.DeltaManipulation;
            return (deltaManipulation.Expansion.X < -15) && (deltaManipulation.Expansion.Y < -15);            
        }        

        // Private members
        private ApplicationViewModel _applicationViewModel;
        private CanvasSubsetControl _canvasSubsetControl;
        private RadialViewControl _radialViewControl;
    }
}
